package com.zls.mapper;

import com.zls.dto.ContractorDto;
import com.zls.pojo.Contractor;
import com.zls.vo.ConProVo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface ContractorMapper {

    @Results(id = "conMap",value = {
            @Result(column = "cid",property = "cid"),
            @Result(column = "cname",property = "cname"),
            @Result(column = "updatetime",property = "updatetime"),
            @Result(column = "cid",property = "projects",many =   //一对多映射
                    //select 关联查询
            @Many(select ="com.com.zls.mapper.ProjectMapper.selectByCids" ,fetchType = FetchType.EAGER)
            )
    })
    @Select("select * from contractor where cid=#{cid} and status=1")
    Contractor findByCid (String cid);

    @Select("Select * from contractor where status =1")
    List<Contractor> findAll();

    @Select("<script>" +
            "select * from contractor where status =1" +
            "<if test='queryString!=null'> and cname like concat( '%',#{queryString},'%')" +
            "limit #{currentPage},#{pageSize}</if>" +
            "<if test='queryString==null'>limit #{currentPage},#{pageSize}</if> "+
            "</script>")
    List<Contractor> findCon(@Param("currentPage") Integer currentPage, @Param("pageSize") Integer pageSize, @Param("queryString") String queryString);

    //查询总记录数
    @Select("<script>" +
            "select count(*) from contractor where status =1" +
            "<if test='queryString!=null'> and cname like concat( '%',#{queryString},'%') </if>" +
            "</script>")
    Long count(@Param("queryString") String queryString);

    //添加
    @Insert("insert into contractor (cid,cname,updatetime) values(#{cid},#{cname},#{updatetime})")
    int add(Contractor contractor);

    //逻辑删除
    @Update("update contractor set status=0 where cid=#{cid}")
    int deleteCon(String cid);

    //修改
    @Update("update contractor set cname=#{cname},updatetime=#{updatetime} where cid=#{cid} and status =1")
    int updateCon(ConProVo conProVo);
    //dto方式
    @Select("select * from contractor where cid =#{cid} and status=1")
    ContractorDto findByCidDto(String cid);
}
